# 说明：在HTML页面中，当点击超链接或者按钮时，有的会在新的窗口打开页面。

# 实现多窗口切换
# 说明：在Selenium中封装了获取当前窗口句柄、获取所有窗口句柄和切换到指定句柄窗口的方法；
# 句柄：英文handle，窗口的唯一识别码
# 方法：
# 1). driver.current_window_handle --> 获取当前窗口句柄
# 2). driver.window_handles --> 获取所有窗口句柄
# 3). driver.switch_to.window(handle) --> 切换指定句柄窗口
# 导包
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# 打开浏览器
service = Service(r"D://Python314//chromedriver.exe")
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service, options=chrome_options)   # 创建浏览器对象

# 2.输入访问地址（URL）
# driver.get("http://121.43.169.97:8848/pageA.html")
url = r"D:\pythonProject\pythonProject1\注册实例.html";
driver.get(url)

# # 跳转操作
# # # 1). 填写主页面的注册信息
# # # 用户名
# driver.find_element(By.CSS_SELECTOR,"#user").send_keys("admin")
# # # 密码
# driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
# # # 电话号码
# driver.find_element(By.CSS_SELECTOR,"#tel").send_keys("13012345678")
# # # 电子邮件
# driver.find_element(By.CSS_SELECTOR,"#email").send_keys("123@qq.com")
# time.sleep(2)
#
# # # 点击'注册A页面'链接
# driver.find_element(By.PARTIAL_LINK_TEXT,"A网页").click()
# time.sleep(2)
# # # # 用户名
# driver.find_element(By.CSS_SELECTOR,"#userA").send_keys("1111")
# # # # 密码
# driver.find_element(By.CSS_SELECTOR,"#passwordA").send_keys("123456")
# # # 电话号码
# driver.find_element(By.CSS_SELECTOR,"#telA").send_keys("13012345678")
# # # 电子邮件
# driver.find_element(By.CSS_SELECTOR,"#emailA").send_keys("123@qq.com")

# 用户名
driver.find_element(By.CSS_SELECTOR,"#user").send_keys("admin")
# # 密码
driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
# 电话号码
driver.find_element(By.CSS_SELECTOR,"#tel").send_keys("13012345678")
# 电子邮件
driver.find_element(By.CSS_SELECTOR,"#email").send_keys("123@qq.com")
time.sleep(2)

# 1.获取‘表单综合实例.html’当前窗口句柄
# 目的：判断只要不是当前窗口句柄，就一定是新的窗口句柄
current_handle = driver.current_window_handle
print("综合实例当前窗口句柄：", current_handle)

# 2.点击'注册A页面'链接
driver.find_element(By.PARTIAL_LINK_TEXT,"A网页").click()

# 3. 获取所有窗口句柄
handles = driver.window_handles
print("所有窗口句柄：", handles)

# 4. 获取注册A页面对应的窗口句柄，并切
# 判断 不是 '当前窗口句柄'
for i in handles:
    if i != current_handle:
        # 切换
        driver.switch_to.window(i)
        # 用户名
        driver.find_element(By.CSS_SELECTOR,"#userA").send_keys("adminA")
        # # 密码
        driver.find_element(By.CSS_SELECTOR,"#passwordA").send_keys("123456")
        # 电话号码
        driver.find_element(By.CSS_SELECTOR,"#telA").send_keys("15011111111")
        # 电子邮件
        driver.find_element(By.CSS_SELECTOR,"#emailA").send_keys("123456@qq.com")

time.sleep(3)
driver.quit()